package com.cloud.pole.weixin.util.wechat;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.alibaba.fastjson.JSONObject;
import com.cloud.pole.weixin.model.entity.WxApp;

public class WxCommonConstants {
	private final static Logger logger = LoggerFactory.getLogger(WxCommonConstants.class);

	// 微信
	public static String access_token_str = "";
	public static Long access_token_date = 0L;
	public static String jsapi_ticket_str = "";
	public static Long jsapi_ticket_date = 0L;


	public static void updateJsTicketFromWeixin(WxApp wxapp) throws Exception {
		long curDate = new Date().getTime();
		if (access_token_str.equals("") || curDate - access_token_date > 7200000) {
			String accessTokenStr = CommonRequest.doGet("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" 
					+ wxapp.getAppId() + "&secret=" + wxapp.getAppSecret());
			logger.info("accessTokenStr:" + accessTokenStr);
			JSONObject tokenMap = JSONObject.parseObject(accessTokenStr);
			if (tokenMap.containsKey("access_token")) {
				String tokenMapStr = tokenMap.getString("access_token");
				access_token_str = tokenMapStr;
				access_token_date = curDate;
				curDate = new Date().getTime();
				String jsapiTicketStr = CommonRequest.doGet("https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=" + tokenMapStr + "&type=jsapi");
				logger.info("jsapiTicketStr:" + jsapiTicketStr);
				JSONObject ticketMapStr = JSONObject.parseObject(jsapiTicketStr);
				if (ticketMapStr.containsKey("ticket")) {
					String ticketStr = ticketMapStr.getString("ticket");
					jsapi_ticket_str = ticketStr;
					jsapi_ticket_date = curDate;
				}
			}
		} else {
			if (jsapi_ticket_str.equals("") || curDate - jsapi_ticket_date > 7200000) {
				String jsapiTicketStr = CommonRequest.doGet("https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=" + access_token_str + "&type=jsapi");
				logger.info("jsapiTicketStr:" + jsapiTicketStr);
				JSONObject ticketMapStr = JSONObject.parseObject(jsapiTicketStr);
				if (ticketMapStr.containsKey("ticket")) {
					String ticketStr = ticketMapStr.getString("ticket");
					jsapi_ticket_str = ticketStr;
					jsapi_ticket_date = curDate;
				}
			}
		}
	}
	
	public static Map<String, Object> getWxShareInfo(String url, WxApp wxApp){
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			WxCommonConstants.updateJsTicketFromWeixin(wxApp);
			String timestamp = String.valueOf(System.currentTimeMillis()/ 1000);
			String noncestr = "*^%Ro$)(Yao@Jk";
			url = java.net.URLDecoder.decode(url,"utf-8");   
			String signature = getSignature(noncestr, jsapi_ticket_str, timestamp, url);
			System.out.println(jsapi_ticket_str);
			result.put("ticket", jsapi_ticket_str);
			result.put("noncestr", noncestr);
			result.put("timestamp", timestamp);
			result.put("appid", wxApp.getAppId());
			result.put("signature", signature);
			result.put("url", url);
		}catch(Exception e) {
			e.printStackTrace();
		}
		return result;
	}
	
	/**
	 * 生成微信 请求 Signature签名
	 * @param noncestr
	 * @param ticket
	 * @param timestamp
	 * @param url
	 * @return
	 */
	public static String getSignature(String noncestr , String ticket, String timestamp, String url){
		String str = "jsapi_ticket=" + ticket + "&noncestr=" + noncestr + "&timestamp=" + timestamp + "&url=" + url;
		return CryptoSHA1.encode(str);
	}
	
}
